

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>载荷列表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="__PUBLIC__/style/admin.css" media="all">
</head>
<body>  
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-header">载荷列表</div>
          <div class="layui-card-body">
            <blockquote class="layui-elem-quote layui-quote-nm">
              <p style="color:#ff5722">【说明】</p>
              <p>自定义载荷需要符合代码规范，多个载荷会合并到模板中一次执行，合并提交结果。</p>              
              <p>最终的脚本会以 <span style="color:#1e9fff">[字段]=[值]&[字段]=[值]</span> 的形式提交给服务器。</p>
              <p style="color:#ff5722">【Payload编写规范】</p>
              <p>Payload分为两部分，分别是字段名和代码。</p>
              <p> <span style="color:#1e9fff">字段名</span>：对应提交请求的[字段]，<span style="color:#ff5722">字段名唯一</span>。</p>
              <p> <span style="color:#1e9fff">代码</span>：对应提交请求的[值]，格式必须为立即执行(IIFE)函数，参考格式：(function() { try{ return 'result' } catch (e) { return '' } })()</p>
            </blockquote>
            <div class="test-table-reload-btn" style="margin-bottom: 10px;">
              <div class="layui-form">
                <div class="layui-inline">
                  <input class="layui-input" name="keyword" autocomplete="off" placeholder="搜索载荷名称">
                </div>
                <button class="layui-btn" lay-submit lay-filter="search-submit">搜索</button>
                <button class="layui-btn layui-bg-red" id="add-payload-btn">添加载荷</button>
              </div>
            </div>
            <table class="layui-hide" id="payload-table"></table>
          </div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="__PUBLIC__/layui/layui.js"></script>

  <script type="text/html" id="actionTpl">
    <button type="button" class="layui-btn layui-btn-xs" onclick="edit_payload('{{d.id}}')">修改</button>
    <button type="button" class="layui-btn layui-bg-red layui-btn-xs" onclick="del_payload('{{d.id}}')">删除</button>
  </script>

  <script>
  var currPage = 1;
  layui.config({
    base: '__PUBLIC__/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口载荷
  }).use(['index', 'table', 'form','util'], function(){
    var table = layui.table,form = layui.form,$ = layui.$, util = layui.util;
  
    //动态加载荷列表
    table.render({
      elem: '#payload-table'
      ,url: "{:U('Ajax/ajax_payload_list')}"
      ,limit: 20 
      ,page: true
      ,cols: [[
        {field:'id', width:80, title: 'ID'}
        ,{field:'name', width:280, title: '载荷名称'}
        ,{field:'desc',  title: '载荷描述'}
        ,{field:'key', width:160, title: '字段名'}
        ,{field:'time', width:160, title: '创建时间', templet:
          function(d){ return util.toDateString(d.time*1000, 'yyyy-MM-dd');}
        }
        ,{field: '', title: '操作', width: 130, templet: '#actionTpl'}
      ]]
      ,cellExpandedMode:'tips'
      ,done: function(res, curr, count){
        currPage = curr;
      }
    });

    // 搜索载荷
    form.on('submit(search-submit)', function(obj){
      var tableIns = table.render({
        elem: '#payload-table'
        ,limit: 20
        ,url: "{:U('Ajax/ajax_payload_list')}"
        ,where: obj.field
        ,page: true
        ,cols: [[
          {field:'id', width:80, title: 'ID'}
          ,{field:'name', width:280, title: '载荷名称'}
          ,{field:'desc',  title: '载荷描述'}
          ,{field:'key', width:160, title: '字段名'}
          ,{field:'time', width:160, title: '创建时间', templet:
            function(d){ return util.toDateString(d.time*1000, 'yyyy-MM-dd');}
          }
          ,{field: '', title: '操作', width: 130, templet: '#actionTpl'}
        ]]
        ,cellExpandedMode:'tips'
        ,done: function(res, curr, count){
          currPage = curr;
        }
      });
    });

    //添加载荷
    $('#add-payload-btn').on('click', function() {
      layer.open({
        type: 2,
        area: ['90%', '90%'],
        maxmin: true,
        title: '添加载荷',
        content: "{:U('Payload/payload_add')}"
      });
    });
  });
  //删除载荷
  function del_payload(id){
    layer.confirm('你确定要删除这个载荷吗？', {
        title: '删除确认',
        btn: ['确定', '取消']
    }, function(index, layero) {
      layui.$.post("{:U('Ajax/ajax_payload_delete')}" , JSON.stringify({"id":id}) ,function(d){
        layer.msg(d.msg);
        if(d.code == 0){refreshTable();}
      },"json");
      layer.close(index);
    }, function(index, layero) {
      layer.close(index);
    });
    }
    //编辑载荷
    function edit_payload(id){
      event.preventDefault();
      layer.open({
        type: 2,
        area: ['90%', '90%'],
        maxmin: true,
        title: '修改载荷',
        content: "{:U('Payload/payload_edit')}?id="+id
      });
    }
    //刷新载荷列表
    function refreshTable(){
      layui.table.reload('payload-table', {
          url: "{:U('Ajax/ajax_payload_list')}",
          page: {
            curr: currPage
          }
      });
    }
  </script>
</body>
</html>